#!/bin/sh
# Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

wlan=eth0
eth=eth1

if [ `cat /sys/class/net/$eth/operstate` = 'up' ]
then
    INTERFACE=${eth}
    SYMBOL="&#xf0e8;"
elif [ `cat /sys/class/net/$wlan/operstate` = 'up' ]
then
    INTERFACE=${wlan}
    SYMBOL="&#xf1eb;"
else
    SYMBOL="&#xf127;"
 fi

ISON=$(ping -q -w 1 -c 1 `/sbin/ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo On || echo Off)
ipaddr="$(/sbin/ifconfig | grep inet | grep -v 'inet6\|127' |sed 's/ /,/g' | sed 's/,,,,,,,,/,/' | cut -d',' -f3)"
ipaddr="${ipaddr:-0.0.0.0}"

if [ "$ISON" = "Off" ]; then # no internet? color will turn red
    echo -n "<span foreground='red'>${SYMBOL} No Internet</span>"
    exit 33
else
    echo -n "<span foreground='#729FCF'>$SYMBOL</span> "
    echo -n $ipaddr
    exit 0
fi
